//location departemenet
function showCarte($top,$left,$posy,$height,iddiv){
	$div=$('#'+iddiv);
	$div.hide();
	$div.css({
		top: $top+'px',
		left: $left+'px',
		backgroundPosition:'0px '+$posy+'px',
		height: $height+'px'
	});
	$div.show();
}

function showImgRegion($top,$left,$posy,$height,$width,$codeCodeRegion){

	$('#carteRegion').width($width);
	$('#search_region option:selected').attr('selected', '');
	$('#search_region').val($codeCodeRegion);
	showCarte($top,$left,$posy,$height,'carteRegion');
	$("#carteRegion").mouseout( function() { $('#carteRegion').hide();} );
}

$(".linkRegion a").mouseout( function() { $('#carteRegion').hide();} );

$(".carte map area").mouseout( function() { $('#carteRegion').hide();} );


function showLocalisation($id,$value){
	$.post("/localisation/index", {name:$id, id:$value } ).
		done(function($data) {
			$('#'+$id).html($data);
			if($id=='ville') $('#code_postal').val($value);
		});
}
//navigator bar search code postal

	$('.search_cp').keyup(function(){
		if($(this).val().length==2){
			$.post("/annonce/gerer/ajax_ville",{ departement: $(this).val()}).done(
					   function(data){
						   $("#search_cp").html(data);
					   }); 
			CodeDept=$(this).val();
			changeRegion();
		}
	});

//Upload Photo//

var fileTemplate="<div id=\"{{id}}\" >";
	fileTemplate+="<div class=\"preview relative\"><div class=\"progressbar\"></div></div>";
	fileTemplate+="</div>";
	$html5Uploader={
		
		onClientLoadStart:function(e,file){
			var upload=$("#zonephoto");
				if(upload.is(":hidden")){
					upload.show();
				}
				upload.append(fileTemplate.replace(/{{id}}/g,slugify(file.name)).replace(/{{filename}}/g,file.name));
			},
		onClientLoad:function(e,file){
				$("#"+slugify(file.name)).find(".preview").append("<img src=\""+e.target.result+"\" alt=\"\">");
			},
			
		onServerLoadStart:function(e,file){
				$("#"+slugify(file.name)).find(".progressbar").progressbar({value:0});
			},

		onServerProgress:function(e,file){
			if(e.lengthComputable){
				var percentComplete=(e.loaded/e.total)*100;
					$("#"+slugify(file.name)).find(".progressbar").progressbar({value:percentComplete});
			}
		},
		onServerLoad:function(e,file){
			$("#"+slugify(file.name)).find(".progressbar").progressbar({value:100}).hide('slow');
		},
		onSuccess:function(e, file, responseText){
			$("#zonephoto").append(responseText);
		}
	};
	
	
	
//mot de passe oublie
	
	$(".annonce-creer #password").change(function() {
		//ajax post email
		$.post("../ajax_check_password",
				 { password: $(this).val() , id: $('#id').val() },
				   function(data){
					   $(".annonce-creer #sendMail").html(data);
					   refresh();
				   }
				 );
		
  		
	  });
	function refresh(){
		$("#oublie_pw").click(function() {
			//ajax post email
			$.post("../ajax_send_password",
					{id: $('#id').val() },
					   function(data){
						   $("#sendMail").html("Un email vient de vous être envoyé. Vous y trouverez les informations pour modifier votre mot de passe.");
					   }
					 );
		  });
	}
	refresh();
//supprimer les photo
	function delPhoto($this,$idPhoto,$photo){
		$this.html("<input name='delPhoto[]' value='"+$idPhoto+"' />");
		$this.parent().hide('slow');
	}
//photo

	//page annonce click on photo

	if ( $( '.info-annonce .thumbnails .thumbnail ' ).length ){

		$('.thumbnails .thumbnail img').click(function() {
			var src=$(this).attr("src").replace('/thumbnails/', '/');
			$('.wrap-image img').attr("src",src);
		});
		
		//click on button left right
		imgThubms = [];
	    $('.info-annonce ul.thumbnails li.thumbnail img').each(function() {
	    	imgThubms.push($(this).attr("src").replace('/thumbnails/', '/'));
		});
	    current=0;
	    function carousel_control(control){
	    	imgSrc=$("#Carousel img").attr("src");
		    $.each(imgThubms, function(index, value) {
		    	if(value==imgSrc)current=index;
		    });
		    out=current + control;
		    if(out<0)out=imgThubms.length-1;
		    if(out>=imgThubms.length)out=0;
		    return src=imgThubms[out];
	    }
		$("#Carousel .carousel-control.right").click(function() {
			src=carousel_control(1);
		    $('.wrap-image img').attr("src",src);
		}); 
		$("#Carousel img").click(function() {
			src=carousel_control(1);
		    $('.wrap-image img').attr("src",src);
		}); 
		$("#Carousel .carousel-control.left").click(function() {
			src=carousel_control(-1);
		    $('.wrap-image img').attr("src",src);
		}); 
		var widthThumb=0;
		var marginLeft=40;
		//width thumbnail minimum par default 200px
		var widthPreview=(200+marginLeft)*$('.info-annonce ul.thumbnails li.thumbnail').length;
		$('.info-annonce ul.thumbnails').width();
		$('.info-annonce ul.thumbnails li.thumbnail').each(function() {
			widthThumb+=$(this).innerWidth() + marginLeft;
		});
		if(widthThumb<widthPreview)widthThumb=widthPreview;
		$('.info-annonce ul.thumbnails').width(widthThumb);
	}

//annonce creer

	$(".annonce-creer input").click(function() {
	    $("#btn-creer-submit").removeClass("disabled");
	}); 
	
//book mark
	function addBookmark(url) {
		var title='Caverne - Une idée, une trouvaille près de chez vous !';
		if (window.sidebar)
			return window.sidebar.addPanel(title, url, "");
		else if ( window.external )
			return window.external.AddFavorite( url, title);
		else if (window.opera && window.print)
			return true;
		return true;
	}
//filtre
//attention sur serveur on met //setting/option/getFilters sur localhost /caverne/setting/option/getFilters
	$('.search-category').change(function(){

		$.post("/setting/option/getFilters", {idOption:$(this).val()} ).
			done(function($data) {
				$('#filters').hide();	
				$('#filter').html('');
				if($.trim($data)!=''){
					$('#filters').show('slow');					
					$('#filter').html($data);
				}
			});
	});

//tooltip
	$('.btn-deposer').tooltip();
//button check mes annonces
	$('.btn-check').tooltip();
	$(".btn-check").click(function() {
	    $(this).removeClass("btn-warning");
	    $(this).addClass("btn-success");
	});
//synchro le menu avec moteur de rechercher
	$('menu.nav a').mouseover(function() {
		$cat_ID=$(this).attr('id');
		$('.search-category').val($cat_ID);
	});